import axios from 'axios';
import qs from 'query-string';
import type { HttpResponse } from './interceptor';

export interface AnnId {
    id: number;
}

export interface AnnRecord {
    id: number;
    ann_title: string;
    ann_content: string;
    created_at: string;
    updated_at: string;
}

export interface AnnListRes {
    list: AnnRecord[];
    total: number;
}

export interface AnnModel {
    id?: number;
    ann_title: string;
    ann_content: string;
}

export interface AnnParams extends Partial<AnnRecord> {
    page: number;
    pageSize: number;
}

/**
 * 获取公告列表
 * @param params
 */
export function queryAnnList(params: AnnParams): Promise<HttpResponse<AnnListRes>> {
    return axios.get('/admin/ann/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取公告详情
 * @param params
 */
export function queryAnnOne(params: AnnId): Promise<HttpResponse<AnnRecord>> {
    return axios.get('/admin/ann/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 提交公告表单（创建/更新）
 * @param data
 */
export function submitAnnForm(data: AnnModel): Promise<HttpResponse<AnnRecord>> {
    return axios.post('/admin/ann/submit', data);
}

/**
 * 删除公告
 * @param id
 */
export function delAnn(id: number): Promise<HttpResponse<null>> {
    return axios.post('/admin/ann/delete', { id });
} 